// 顶部导航栏的自动隐藏
$J(function() {
    var header = $J('.jmall-header-wrap.jmall-fixed, .jmall-header-wrap.jmall-extend'),
        _h = header.find('.jmall-menu').outerHeight() + 2;
    if(!header.length) return;
    $J(window).scroll(function(){
        if($J(window).scrollTop() < _h){
            header.css({'position':'absolute', 'top':'0'});
            header.removeClass('jmall-header-now-fixed');
        }
        else{
            header.css({'position':'fixed', 'top': - + _h + 'px'});
            header.addClass('jmall-header-now-fixed');
        }
    });
});

// 回到顶部
$J('.go-top').click(function(){
    $J('body, html').animate({'scrollTop': 0}, 'slow');
});

//客服按钮
var tora_img = 'http://chatserver.comm100.cn/BBS.aspx?siteId=80021157&planId=1143&partnerId=-1&r=' + Math.random();
$J('<img src="' + tora_img + '">').load(function(){
    $J(".jmall-header-wrap .jmall-cs span").css("background-image", "url('" + tora_img + "')");
});
$J(".jmall-header-wrap a.jmall-cs").click(function() {
    if(!$J(this).data('jump')){
        $J("#jmall-before-ask").show();
        ga('send', 'event', 'top', 'cs_popup');
        return false;
    }
});
$J("#jmall-before-ask a.jmall-submit").click(function() {
    $J("#jmall-before-ask").hide();
    return false;
});

$J('.jmall-header-container').on('click', 'a', function(){
    var path = $J(this).attr('href').match(/^https?:\/\/[^\/]+(\/.*)$/), action, tag;
    if( path ){
        if( $J(this).hasClass('icon') ){
            action = '/amazon';
            tag = $J(this).attr('class').replace('icon type-', '');
        } else if( path[1].indexOf('/amazon/') >= 0 ){
            action = '/amazon';
            tag = path[1];
        } else if( path[1].indexOf('/login') >= 0 ) {
            action = '/login';
            tag = path[1];
        } else {
            action = path[1];
            tag = '';
        }
        ga('send', 'event', 'top', action, tag);
    }
});

// 切换搜索来源
function init_switch_search_source() {
	var current = $J('.jmall-current-source');
	var current_id = $J('#search-source-id');
	$J('.jmall-source-list').on('click', 'li', function(){
		current_id.val($J(this).data('source-site'));
		current.text($J(this).text());
		var layer = $J(this).parent();
		layer.css('left', '-9999px');
		setTimeout(function(){
			layer.css('left', '0');
		}, 300);
	});
}
init_switch_search_source();